package com.example.yt_app.Activity.Welcome;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.yt_app.Activity.BaseActivity.BaseActivity;
import com.example.yt_app.Activity.FragmentThreeActivity;
import com.example.yt_app.Activity.Login;
import com.example.yt_app.Activity.SetActivity;
import com.example.yt_app.AppManager;
import com.example.yt_app.R;
import com.example.yt_app.Urils.SPTool;
import com.example.yt_app.Urils.State;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;
import site.gemus.openingstartanimation.LineDrawStrategy;
import site.gemus.openingstartanimation.NormalDrawStrategy;
import site.gemus.openingstartanimation.OpeningStartAnimation;
import site.gemus.openingstartanimation.RedYellowBlueDrawStrategy;
import site.gemus.openingstartanimation.RotationDrawStrategy;

/**
 * Created by dell on 2021/7/15.
 */
@SuppressWarnings("ALL")
public class SplashActivity extends BaseActivity {
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @BindView(R.id.tv_time)
    TextView tv_time;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_splash;
    }
    //是否是第一次使用
    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
    private static boolean isFirstUse ;
    private OpeningStartAnimation openingStartAnimation;
    @Override
    protected void initView() {
        AppManager.getInstance().addActivity(this);
        SharedPreferences preferences = getSharedPreferences("isFirstUse", MODE_PRIVATE);
        isFirstUse = preferences.getBoolean("isFirstUse", true);

        if (isFirstUse) {
            startActivity(new Intent(this, GuideActivity.class));

            //实例化Editor对象
            SharedPreferences.Editor editor = preferences.edit();
            //存入数据
            editor.putBoolean("isFirstUse", false);
            //提交修改
            editor.commit();
        } else {
            State.ApiKey = SPTool.getInstance(this).getString("Api_Key","FN8vCnpwAjyjTV2m1y=zpL451i4=");
            System.out.println(State.ApiKey);
            //隐藏状态栏
            ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_STATUS_BAR).init();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    timer.start();
                }
            }, 500);
        }
        OpenApp();

    }
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @SuppressLint("Range")
    void OpenApp(){
        openingStartAnimation = new OpeningStartAnimation.Builder(this)
                .setDrawStategy(new NormalDrawStrategy())
                .setAppIcon(getResources().getDrawable(R.mipmap.ic_launcher_foreground)) //设置图标
                .setColorOfAppIcon(Color.RED) //设置绘制图标线条的颜色
                .setAppName("Hot Home") //设置app名称
                .setColorOfAppName(Color.DKGRAY) //设置app名称颜色
                .setAppStatement("敢想 敢言 敢做") //设置一句话描述
                .setColorOfAppStatement(Color.BLUE) // 设置一句话描述的颜色
                .setAnimationInterval(3000) // 设置动画时间间隔
                .setAnimationFinishTime(3000) // 设置动画的消失时长
                .create();
        openingStartAnimation.show(this);
        }
    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @OnClick(R.id.tv_time)
    void  OnClick_tv_time(){
        if (State.RESULT == false){

            Intent intent = new Intent(SplashActivity.this, FragmentThreeActivity.class);
            startActivity(intent);
            State.RESULT = true;
            finish();
        }

    }

    /**
     * CountDownTimer timer = new CountDownTimer(3000, 1000)中，
     * 第一个参数表示总时间，第二个参数表示间隔时间。
     * 意思就是每隔一秒会回调一次方法onTick，然后1秒之后会回调onFinish方法。
     */
    CountDownTimer timer = new CountDownTimer(6000, 500) {
        @Override
        public void onTick(long millisUntilFinished) {
            tv_time.setText("点击我跳过界面  倒计时" + millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            if (State.RESULT == false) {
                Intent intent = new Intent(SplashActivity.this, FragmentThreeActivity.class);
                startActivity(intent);
                finish();
            }

        }

    };
    //调用 CountDownTimer 对象的 start() 方法开始倒计时，也不涉及到线程处理
    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
    @Override
    protected void onDestroy(){
        super.onDestroy();
        AppManager.getInstance().removeActivity(this);
    }


}